import {DEFAULT_PAGE_SIZE} from '@/util/constant'

const state = {
  showSlider: true,
  showLoading: false,
  getRequestCount: 0,
  responseErrorStatus: false,
  pageSize: DEFAULT_PAGE_SIZE,
  currentPage: 1,
  total: 0,
  tableData: [],
  showDialog: false,
  searchModel: {},
  updateModel: {}
}

const getters = {
  showSliderGetters: (state) => state.showSlider,
  showLoadingGetters: (state) => state.showLoading,
  pageSizeGetters: (state) => state.pageSize,
  currentPageGetters: (state) => state.currentPage,
  tableDataGetters: (state) => state.tableData,
  searchModelGetters: (state) => state.searchModel,
  totalGetters: (state) => state.total,
  showDialogGetters: (state) => state.showDialog,
  updateModelGetters: (state) => state.updateModel
}

const mutations = {
  changeShowSlider (state) {
    state.showSlider = !state.showSlider
  },
  countRequest (state) {
    state.showLoading = true
    state.getRequestCount++
  },
  countResponse (state) {
    state.getRequestCount--
    if (state.getRequestCount === 0) {
      state.showLoading = false
    }
  },
  responseError (state) {
    state.responseErrorStatus = true
  },
  changePageSize (state, pageSize) {
    state.pageSize = pageSize
  },
  changeCurrentPage (state, currentPage) {
    state.currentPage = currentPage
  },
  changeTableData (state, data) {
    state.tableData = data
  },
  changeTotal (state, total) {
    state.total = total
  },
  changeUpdateModel (state, updateModel) {
    state.updateModel = updateModel
  },
  openDialog (state) {
    state.showDialog = true
  },
  closeDialog (state) {
    state.showDialog = false
  },

  intiComponentData (state) {
    state.currentPage = 1
    state.total = 0
    state.tableData = []
    state.showDialog = false
    state.searchModel = {}
    state.updateModel = {}
  }
}

export default {
  state,
  getters,
  mutations
}
